package com.hubing.cloud.app.web.service.impl;

import com.google.common.collect.Lists;
import com.hubing.cloud.app.web.mapper.MenuMapper;
import com.hubing.cloud.app.web.model.GlobalException;
import com.hubing.cloud.app.web.model.dto.MenuDto;
import com.hubing.cloud.app.web.model.entity.Menu;
import com.hubing.cloud.app.web.model.entity.Navigation;
import com.hubing.cloud.app.web.model.vo.MenuVO;
import com.hubing.cloud.app.web.service.MenuService;
import com.hubing.cloud.app.web.util.IdUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 81517
* @description 针对表【menu】的数据库操作Service实现
* @createDate 2023-01-31 01:42:54
*/
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 查询 - 树状菜单
     *
     * @return
     */
    @Override
    public List<MenuVO> queryMenu() {

        Map<Boolean, List<MenuVO>> map = menuMapper.queryMenu().stream().collect(Collectors.partitioningBy(item ->null==item.getParentId()));

        // 根菜单
        List<MenuVO> rootList = map.get(true);

        // 非根菜单
        Map<String, List<MenuVO>> noRootList = map.get(false).stream().collect(Collectors.toMap(MenuVO::getParentId,
                Lists::newArrayList,
                (List<MenuVO> l1, List<MenuVO> l2) -> {
                    l2.addAll(l1);
                    return l2;
                }));

        // 包装子节点
        queryChild(rootList,noRootList);

        return rootList;
    }

    /**
     * 添加 - 菜单
     *
     *@param dto
     * @return
     */
    @Override
    public Boolean addMenu(MenuDto dto) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("menuId",dto.getParentId());
        Menu parent = menuMapper.selectOneByExample(example);

        Integer maxSort = menuMapper.queryMaxSort(parent.getMenuId());

        Menu menu = new Menu();
        BeanUtils.copyProperties(dto,menu);
        menu.setMenuId(IdUtil.getIncreaseIdByCurrentTimeMillis());
        menu.setMenuSort(++maxSort);
        menu.setMenuType(1);
        menu.setCreated(new Date());
        menu.setVersion(1L);
        menu.setMenuStatus(1);
        menu.setRootMenuId(null==parent.getParentId() ? parent.getMenuId() : parent.getRootMenuId());
        menu.setMenuDefault(0);
        return true;
    }

    /**
     * 删除 - 菜单
     *
     * @param menuId
     * @return
     */
    @Override
    public Boolean deleteMenu(String menuId) {
        Example example = new Example(Menu.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("parentId",menuId);
        int count = menuMapper.selectCountByExample(example);
        if (count > 0) {
            throw new GlobalException("菜单下有内容，不可删除！");
        }

        // 删除菜单
        Menu menu = new Menu();
        menu.setMenuId(menuId);
        menu.setMenuStatus(0);
        menuMapper.updateByPrimaryKeySelective(menu);
        return true;
    }

    private void queryChild(List<MenuVO> list, Map<String, List<MenuVO>> noRootList) {
        if (CollectionUtils.isNotEmpty(list)) {
            for (MenuVO item: list) {
                List<MenuVO> childList = noRootList.get(item.getMenuId());
                item.setChild(childList);
                noRootList.remove(item.getMenuId());

                queryChild(childList, noRootList);
            }
        }
    }

    /**
     * 查询 - 一级菜单
     *
     * @return
     */
    @Override
    public List<MenuVO> queryRootMenu() {
        List<MenuVO> list = menuMapper.queryRootMenu();
        return list;
    }

    /**
     * 主键查询菜单
     *
     * @param menuId
     * @return
     */
    @Override
    public List<MenuVO> queryByIds(List<String> menuId) {
        if (CollectionUtils.isEmpty(menuId)) {
            return new ArrayList<>();
        }
        return menuMapper.queryByMenuIdIn(menuId);
    }
}
